#!/usr/bin/env bash
set -eu
. common
set -- ${cli_run:-} "$@"

# CLI handling.
cpus=1
debug_vm=
debug=false
kgdb=false
kvm=false
# norandmaps: Don't use address space randomization. Equivalent to echo 0 > /proc/sys/kernel/randomize_va_space.
# nokaslr:
# - https://unix.stackexchange.com/questions/397939/turning-off-kaslr-to-debug-linux-kernel-using-qemu-and-gdb
# - https://stackoverflow.com/questions/44612822/unable-to-debug-kernel-with-qemu-gdb/49840927#49840927
#   Turned on by default since v4.12
extra_append='console_msg_format=syslog nokaslr norandmaps printk.devkmsg=on printk.time=y'
extra_append_after_dash=
extra_flags=
extra_flags_qemu=
extra_opts=
gem5=false
gem5opts=
gem5_fsbiglittle=false
lkmc_eval=
initrd=false
initramfs=false
memory=256M
nographic=true
prebuilt=false
root=
tmux=false
tmux_args=
trace_enabled=false
# A dummy value that is already turned on by default and does not produce large output,
# just to prevent QEMU from emitting a warning that '' is not valid.
trace_type=pr_manager_run
vnc=
while getopts a:c:DdE:e:F:f:G:ghIiKkL:M:m:N:n:PT:t:U:uVX:x OPT; do
  case "$OPT" in
    a)
      arch="$OPTARG"
      ;;
    c)
      cpus="$OPTARG"
      ;;
    D)
      debug_vm="gdb -q -ex start --args \\
"
      ;;
    d)
      debug=true
      extra_flags_qemu="$extra_flags_qemu -S \\
"
      ;;
    E)
      lkmc_eval="$OPTARG"
      ;;
    e)
      extra_append="${extra_append} ${OPTARG}"
      ;;
    F)
      extra_append_after_dash="${extra_append_after_dash} lkmc_eval_base64=\"$(printf "${OPTARG}" | base64)\""
       ;;
    f)
      extra_append_after_dash="${extra_append_after_dash} ${OPTARG}"
      ;;
    G)
      gem5opts="$OPTARG \\
"
      ;;
    g)
      gem5=true
      ;;
    h)
      cat build-usage.adoc 1>&2
      exit
      ;;
    I)
      initramfs=true
      ;;
    i)
      initrd=true
      ;;
    K)
      kvm=true
      ;;
    k)
      extra_append="$extra_append kgdbwait"
      # For those who want to try KDB.
      #extra_append="$extra_append kgdbwait kgdboc=kbd"
      kgdb=true
      ;;
    L)
      common_linux_variant="$OPTARG"
      ;;
    M)
      common_gem5_variant="$OPTARG"
      ;;
    m)
      memory="$OPTARG"
      ;;
    N)
      common_gem5_worktree="$OPTARG"
      ;;
    n)
      common_run_id="$OPTARG"
      ;;
    P)
      prebuilt=true
      ;;
    T)
      trace_enabled=true
      trace_type="$OPTARG"
      ;;
    t)
      common_gem5_build_type="$OPTARG"
      ;;
    U)
      tmux_args="$OPTARG"
      ;;
    u)
      tmux=true
      ;;
    X)
      extra_opts="${extra_opts} ${OPTARG}"
      ;;
    x)
      nographic=false
      ;;
    V)
      vnc="-vnc :0 \\
"
      ;;
    ?)
      exit 2
      ;;
  esac
done
shift "$(($OPTIND - 1))"
extra_flags="$extra_flags $@"
OPTIND=1
if [ -n "$extra_opts" ]; then
  while getopts b OPT $extra_opts; do
    case "$OPT" in
      b)
        gem5_fsbiglittle=true
        ;;
      ?)
        exit 2
        ;;
    esac
  done
fi
set_common_vars -L "$common_linux_variant" -M "$common_gem5_variant" -n "$common_run_id" "$arch" "$gem5"
if "$debug" && "$kvm"; then
  echo 'error: -d and -K are incompatible' 1>&2
  exit 1
fi
if "$initrd" || "$initramfs"; then
  ramfs=true
else
  ramfs=false
fi
if [ -n "$lkmc_eval" ]; then
  if "$ramfs"; then
    initarg="rdinit"
  else
    initarg="init"
  fi
  extra_append="${extra_append} ${initarg}=/eval_base64.sh"
  extra_append_after_dash="${extra_append_after_dash} lkmc_eval=\"$(printf "$lkmc_eval" | base64)\""
fi
if "$nographic"; then
  if [ "$arch" = x86_64 ]; then
    extra_append="${extra_append} console=ttyS0"
  fi
  extra_flags_qemu="${extra_flags_qemu} -nographic \\
"
fi
if [ -n "$extra_append_after_dash" ]; then
  extra_append="${extra_append} - ${extra_append_after_dash}"
fi

if "$gem5"; then
  memory="${memory}B"
  if [ "$arch" = x86_64 ]; then
    gem5_arch=X86
  else
    gem5_arch=ARM
  fi
  if "$trace_enabled"; then
    gem5opts="${gem5opts} --debug-flags='${trace_type}' \\
"
  fi
  gem5_common="\
M5_PATH='${common_gem5_system_dir}' \\
${debug_vm} \
'${common_gem5_build_dir}/${gem5_arch}/gem5.${common_gem5_build_type}' \\
--debug-file=trace.txt \\
${gem5opts} \
-d '${common_m5out_dir}' \\
"
  if "$gem5_fsbiglittle"; then
      cmd="${gem5_common} \
"${common_gem5_default_src_dir}/configs/example/arm/fs_bigLITTLE.py" \
--big-cpus=2 \\
--cpu-type=atomic \\
--disk="${common_images_dir}/rootfs.ext2" \\
--dtb "${common_gem5_system_dir}/arm/dt/armv8_gem5_v1_big_little_2_2.dtb" \\
--kernel="${common_vmlinux}" \\
--little-cpus=2 \\
${extra_flags} \\
"
  else
    gem5_common="\
${gem5_common} \
'${common_gem5_src_dir}/configs/example/fs.py' \\
--disk-image='${common_images_dir}/rootfs.ext2' \\
--kernel='${common_vmlinux}' \\
--mem-size='${memory}' \\
--num-cpus='${cpus}' \\
--script='${readfile_file}' \\
"
    if [ "$arch" = x86_64 ]; then
      if "$kvm"; then
        extra_flags="$extra_flags --cpu-type=X86KvmCPU"
      fi
      cmd="\
${gem5_common} \
--command-line='earlyprintk=ttyS0 console=ttyS0 lpj=7999923 root=/dev/sda ${extra_append}' \\
${extra_flags} \
"
    elif [ "$arch" = arm ] || [ "$arch" = aarch64 ]; then
      # TODO why is it mandatory to pass mem= here? Not true for QEMU.
      # Anything smaller than physical blows up as expected, but why can't it auto-detect the right value?
      cmd="${gem5_common} \
--command-line='earlyprintk=pl011,0x1c090000 console=ttyAMA0 lpj=19988480 rw loglevel=8 mem=${memory} root=/dev/sda ${extra_append}' \\
--dtb-file='${common_gem5_system_dir}/arm/dt/$([ "$arch" = arm ] && echo "armv7_gem5_v1_${cpus}cpu" || echo "armv8_gem5_v1_${cpus}cpu").dtb' \\
--machine-type=VExpress_GEM5_V1 \\
${extra_flags} \
"
    fi
  fi
else
  mkdir -p "$common_qemu_run_dir"
  if [ -z "$debug_vm" ]; then
    serial_monitor='-serial mon:stdio \
'
  else
    serial_monitor=
  fi
  if "$kvm"; then
    extra_flags="${extra_flags} -enable-kvm"
  fi
  if "$kgdb"; then
    extra_flags_qemu="$extra_flags_qemu -serial 'tcp::${common_gdb_port},server,nowait' \\
"
  fi
  if "$prebuilt"; then
    common_mkdir
    qemu_executable="qemu-system-${arch}"
  else
    qemu_executable="${buildroot_out_dir}/host/usr/bin/qemu-system-${arch}"
  fi
  extra_flags="${extra_flags_qemu} ${extra_flags}"
  qemu_common="\
${debug_vm} \
${qemu_executable} \\
-device rtl8139,netdev=net0 \\
-gdb 'tcp::${common_gdb_port}' \\
-kernel '${common_linux_image}' \\
-m '${memory}' \\
-monitor 'telnet::${common_qemu_monitor_port},server,nowait' \\
-netdev 'user,hostfwd=tcp::${common_qemu_hostfwd_generic_port}-:${common_qemu_hostfwd_generic_port},hostfwd=tcp::${common_qemu_hostfwd_ssh_port}-:22,id=net0' \\
${serial_monitor} \
-smp '${cpus}' \\
-trace 'enable=${trace_type},file=${common_qemu_run_dir}/trace.bin' \\
-virtfs 'local,path=${p9_dir},mount_tag=host_scratch,security_model=mapped,id=host_scratch' \\
-virtfs 'local,path=${buildroot_out_dir}/build,mount_tag=host_out,security_model=mapped,id=host_out' \\
${vnc}"
  if "$initrd"; then
    extra_flags="${extra_flags} -initrd '${common_images_dir}/rootfs.cpio' \\
"
  fi
  if "$ramfs"; then
    # TODO why is this needed, and why any string works.
    root='root=/dev/anything'
  else
    if [ ! "$arch" = mips64 ]; then
      extra_flags="${extra_flags} -drive 'file=${common_images_dir}/rootfs.ext2.qcow2,format=qcow2,if=virtio,snapshot' \\
"
      root='root=/dev/vda'
    fi
  fi
  case "$arch" in
    x86_64)
      if "$kgdb"; then
        extra_append="${extra_append} kgdboc=ttyS0,115200"
      fi
      if "$prebuilt"; then
        custom_devices=
      else
        custom_devices="-device lkmc_pci_min \\
"
      fi
      cmd="\
${qemu_common} \
-M pc \\
-append '${root} nopat ${extra_append}' \\
-device edu \\
${custom_devices} \\
${extra_flags} \
"
    ;;
    arm)
      if "$kgdb"; then
        extra_append="${extra_append} kgdboc=ttyAMA0,115200"
      fi
      cmd="\
${qemu_common} \
-M virt \\
-append '${root} ${extra_append}' \\
-cpu cortex-a15 \\
-device virtio-gpu-pci \\
${extra_flags} \
"
    ;;
    aarch64)
      if "$kgdb"; then
        extra_append="${extra_append} kgdboc=ttyAMA0,115200"
      fi
      cmd="\
${qemu_common} \
-M virt \\
-append '${root} ${extra_append}' \\
-cpu cortex-a57 \\
-device virtio-gpu-pci \\
-kernel '${common_images_dir}/Image' \\
${extra_flags} \
"
    ;;
    mips64)
      if ! "$ramfs"; then
        root='root=/dev/hda'
        extra_flags="${extra_flags} -drive 'file=${common_images_dir}/rootfs.ext2.qcow2,format=qcow2,snapshot' \\
"
      fi
      cmd="\
${qemu_common} \
-M malta \\
-append '${root} ${extra_append}' \\
-cpu I6400 \\
-kernel '${common_vmlinux}' \\
${extra_flags} \
"
    ;;
  esac
fi
if "$tmux"; then
  if "$gem5"; then
    eval "./tmu 'sleep 2;./gem5-shell -n ${common_run_id} ${tmux_args};'"
  elif  "$debug"; then
    eval "./tmu ./rungdb -a '${arch} -L ${common_linux_variant}' -n ${common_run_id} ${tmux_args}"
  fi
fi
cmd="${cmd} \\
|& tee ${common_termout_file} \
"
"${root_dir}/eeval" "$cmd" "${common_run_dir}/run.sh"
